/**
 * @file Body_Heath_Sensor.h
 * @brief human body health monitoring sensor
 * @author achdu0000@163.com
 * @version 1.0.0
 * @date 2024/07/01
 */

#ifndef _BODY_HEALTH_SENSOR_H_
#define _BODY_HEALTH_SENSOR_H_

#include <Arduino.h>

#define uint8_t unsigned char

/**
 * @brief Use Serial1 on Arduino GIGIA R1
 * @details
 * - Serial1 pins:
 *      0 -> RX ,  1 <- TX
 * - Serial2 pins:
 *     19 -> RX1, 18 <- TX1
 * - Serial3 pins:
 *     17 -> RX2, 16 <- TX2
 * - Serial4 pins:
 *     15 -> RX3, 14 -> TX3
 */
#define _BODY_HEALTH_SERIAL Serial1

/** send start sequence */
#define SEND_START_SEQ()                 \
    ;                                    \
    {                                    \
        _BODY_HEALTH_SERIAL.write(0x8A); \
    }
/** send end sequence */
#define SEND_END_SEQ()                   \
    ;                                    \
    {                                    \
        _BODY_HEALTH_SERIAL.write(0x88); \
    }

/**
 * @brief human body health monitoring sensor data structure(totally, 88 bytes)
 */
typedef struct
{
    uint8_t header;      // 数据头
    int8_t acdata[64]; // 心律波形数据
    uint8_t heartrate; // 心率
    uint8_t spo2;      // 血氧
    uint8_t bk;        // 微循环
    uint8_t fatig;     // 疲劳指数
    uint8_t rsv[2];    // 保留数据
    uint8_t rsv3;      // 收缩压
    uint8_t rsv4;      // 舒张压
    uint8_t rsv5;      // 心输出
    uint8_t rsn6;      // 外周阻力
    uint8_t rsv7;      // rr间期
    uint8_t sdnn;      // 心率变异性
    uint8_t rmssd;     // rmsd
    uint8_t nn50;      // nn50
    uint8_t pnn50;     // pnn50
    uint8_t rra[6];    // rr间期
    uint8_t _rsv;       // 保留
    uint8_t state;     // 模块状态
} BodyHeathSensor;

/**
 * check if the head of BodyHeathSensor is available
 * @param[in] BodyHeathSensor bhs
 * @return [true]: available, [false]: unavailable
 */
bool isAvailable(BodyHeathSensor bhs);

/**
 * @brief setup the config and be used in .ino setup()
 */
void myBodyHealthSensorSetup();

/**
 * @brief use for func myBodyHealthSensorLoop()
 * @details determine whether to SEND_START_SEQ() in ...Loop()
 */
inline bool sendStartSeqFlag = true;

/**
 * @brief get the data from sensor and be used in .ino loop()
 */
void myBodyHeathSensorLoop();

#endif //!_BODY_HEALTH_SENSOR_H_
